package com.ywgt.system.pojo;

import lombok.Data;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

@Data
public class AuthUser implements UserDetails {

    private String username;

    private String password;

    private List<String> perms;

    private boolean enable;


    public AuthUser() {
    }

    public AuthUser(String username, String password, List<String> perms, boolean enable) {
        this.username = username;
        this.password = password;
        this.perms = perms;
        this.enable = enable;
    }

    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        List<GrantedAuthority> authorities=new ArrayList<>();
        for (String perm : perms) {
            if (StringUtils.isEmpty(perm)){continue;}
            SimpleGrantedAuthority simpleGrantedAuthority=new SimpleGrantedAuthority(perm);
            authorities.add(simpleGrantedAuthority);
        }
        return authorities;
    }

    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    @Override
    public boolean isAccountNonLocked() {
        return true;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    @Override
    public boolean isEnabled() {
        return enable;
    }
}
